<!DOCTYPE html>
<html>
<head>
<title>Test attributeChanged callback is called if custom element attribute value is set</title>
<meta name="author" title="Sergey G. Grekhov" href="mailto:sgrekhov@unipro.ru">
<meta name="author" title="Aleksei Yu. Semenov" href="mailto:a.semenov@unipro.ru">
<meta name="assert" content="attributeChanged callback ... must be enqueued whenever custom element's attribute is added, changed or removed.">
<link rel="help" href="http://www.w3.org/TR/custom-elements/#types-of-callbacks">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../testcommon.js"></script>
<link rel="stylesheet" href="/resources/testharness.css">
</head>
<body>
<div id="log"></div>
<script>
test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    var GeneratedConstructor = doc.registerElement('x-a', {prototype: proto});

    var customElement = new GeneratedConstructor();
    customElement.setAttribute('class', 'someClass');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1, 
        'Callback attributeChanged should be called');
}, 'Test attributeChanged callback is called if attribute value is set by method ' +
    'setAttribute(). The custom element is created via constructor.');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    var GeneratedConstructor = doc.registerElement('x-b', {prototype: proto});

    var customElement = new GeneratedConstructor();
    customElement.setAttribute('class', 'someClass');
    assert_array_equals(proto.attributeChangedCallbackArgs, ['class', null, 'someClass'],
        'Unexpected callback attributeChanged arguments');
}, 'Test attributeChanged callback arguments if attribute value is set by method ' +
    'setAttribute(). The custom element is created via constructor.');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    var GeneratedConstructor = doc.registerElement('x-c', {prototype: proto});

    var customElement = new GeneratedConstructor();
    customElement.classList.add('someClass');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1, 
        'Callback attributeChanged should be called');
}, 'Test attributeChanged callback is called if attribute value is set by method ' +
    'classList.add(). The custom element is created via constructor.');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    var GeneratedConstructor = doc.registerElement('x-d', {prototype: proto});

    var customElement = new GeneratedConstructor();
    customElement.classList.add('someClass');
    assert_array_equals(proto.attributeChangedCallbackArgs, ['class', null, 'someClass'],
        'Unexpected callback attributeChanged arguments');
}, 'Test attributeChanged callback arguments if attribute value is set by method ' +
    'classList.add(). The custom element is created via constructor.');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    var GeneratedConstructor = doc.registerElement('x-e', {prototype: proto});

    var customElement = new GeneratedConstructor();
    customElement.classList.toggle('someClass');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1, 
        'Callback attributeChanged should be called');
}, 'Test attributeChanged callback is called if attribute value is set by method ' +
    'classList.toggle(). The custom element is created via constructor.');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    var GeneratedConstructor = doc.registerElement('x-f', {prototype: proto});

    var customElement = new GeneratedConstructor();
    customElement.classList.toggle('someClass');
    assert_array_equals(proto.attributeChangedCallbackArgs, ['class', null, 'someClass'],
        'Unexpected callback attributeChanged arguments');
}, 'Test attributeChanged callback arguments if attribute value is set by method ' +
    'classList.toggle(). The custom element is created via constructor.');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    var GeneratedConstructor = doc.registerElement('x-g', {prototype: proto});

    var customElement = new GeneratedConstructor();
    customElement.className = 'someClass';
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1,
        'Callback attributeChanged should be called');
}, 'Test attributeChanged callback is called if attribute value is set as property. ' +
    'The custom element is created via constructor.');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    var GeneratedConstructor = doc.registerElement('x-h', {prototype: proto});

    var customElement = new GeneratedConstructor();
    customElement.className = 'someClass';
    assert_array_equals(proto.attributeChangedCallbackArgs, ['class', null, 'someClass'],
        'Unexpected callback attributeChanged arguments');
}, 'Test attributeChanged callback arguments if attribute value is set as property. '+
    'The custom element is created via constructor.');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-i', {prototype: proto});

    doc.body.innerHTML = '<x-i id="x-i" class="theClass"></x-i>';
    assert_equals(proto.attributeChangedCallbackCalledCounter, 0, 
        'Callback attributeChanged should not be called');
}, 'Test attributeChanged callback is not called if attribute value is specified in HTML. '+
    'The custom element is created via innerHTML property.');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-j', {prototype: proto});

    doc.body.innerHTML = '<x-j id="x-j" class="theClass"></x-j>';
    var customElement = doc.querySelector('#x-j');
    customElement.setAttribute('class', 'someClass');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1, 
        'Callback attributeChanged should be called');
}, 'Test attributeChanged callback is called if attribute value is set by method setAttribute(). '+
    'The custom element is created via innerHTML property.');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-k', {prototype: proto});

    doc.body.innerHTML = '<x-k id="x-k"></x-k>';
    var customElement = doc.querySelector('#x-k');
    customElement.setAttribute('class', 'someClass');
    assert_array_equals(proto.attributeChangedCallbackArgs, ['class', null, 'someClass'],
        'Unexpected callback attributeChanged arguments');
}, 'Test attributeChanged callback arguments if attribute value is set by method setAttribute(). '+
    'The custom element is created via innerHTML property.');


test(function() {
    var doc = newHTMLDocument();

    doc.body.innerHTML = '<x-l id="x-l"></x-l>';
    var customElement = doc.querySelector('#x-l');
    // this call shouldn't invoke or enqueue the callback
    customElement.setAttribute('class', 'someClass');

    var proto = newHTMLElementPrototype();
    doc.registerElement('x-l', {prototype: proto});
    assert_equals(proto.attributeChangedCallbackCalledCounter, 0, 
        'Callback attributeChanged should not be called');
    // this one should
    customElement.setAttribute('class', 'someClass2');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1, 
        'Callback attributeChanged should be called');
}, 'Test attributeChanged callback is not called if attribute value of unresolved element '+
    'is set by method setAttribute().');


test(function() {
    var doc = newHTMLDocument();
    HTML5_ELEMENTS.forEach(function(element) {
        var obj = doc.createElement(element);
        var proto = newCustomElementPrototype(obj.constructor.prototype);
        var GeneratedConstructor = doc.registerElement('x-' + element + '-' + element + '-1', {
            prototype: proto,
            extends: element
        });

        var customElement = new GeneratedConstructor();
        customElement.setAttribute('class', 'someClass');
        assert_equals(proto.attributeChangedCallbackCalledCounter, 1, 
            'Callback attributeChanged should be called');
    });
}, 'Test attributeChanged callback of the custom element that extends some HTML element. ' +
    'Test that the callback is called');


test(function() {
    var doc = newHTMLDocument();
    HTML5_ELEMENTS.forEach(function(element) {
        var obj = doc.createElement(element);
        var proto = newCustomElementPrototype(obj.constructor.prototype);
        var GeneratedConstructor = doc.registerElement('x-' + element + '-' + element + '-1-1', {
            prototype: proto,
            extends: element
        });

        var customElement = new GeneratedConstructor();
        customElement.classList.add('someClass');
        assert_equals(proto.attributeChangedCallbackCalledCounter, 1, 
            'Callback attributeChanged should be called');
    });
}, 'Test attributeChanged callback is called if attribute is set by method classList.add(). '+
    'The custom element extends some HTML element.');


test(function() {
    var doc = newHTMLDocument();
    HTML5_ELEMENTS.forEach(function(element) {
        var obj = doc.createElement(element);
        var proto = newCustomElementPrototype(obj.constructor.prototype);
        var GeneratedConstructor = doc.registerElement('x-' + element + '-' + element + '-2', {
            prototype: proto,
            extends: element
        });

        var customElement = new GeneratedConstructor();
        customElement.setAttribute('class', 'someClass');
        assert_array_equals(proto.attributeChangedCallbackArgs, ['class', null, 'someClass'],
            'Unexpected callback attributeChanged arguments');
    });
}, 'Test attributeChanged callback arguments if attribute value is set by method setAttribute(). '+
    'The custom element extends some HTML element.');


testInIFrame('../../resources/x-element.html', function(doc) {
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-element', {prototype: proto});

    var customElement = doc.querySelector('#x-element');
    customElement.setAttribute('class', 'someClass');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1, 
        'Callback attributeChanged should be called');
}, 'Test attributeChanged callback is called if attribute value is set by method setAttribute(). '+
    'The document has browsing context.');


testInIFrame('../../resources/x-element.html', function(doc) {
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-element', {prototype: proto});

    var customElement = doc.querySelector('#x-element');
    customElement.setAttribute('class', 'someClass');
    assert_array_equals(proto.attributeChangedCallbackArgs, ['class', null, 'someClass'],
        'Unexpected callback attributeChanged arguments');
}, 'Test attributeChanged callback arguments if attribute value is set by method setAttribute(). '+
    'The document has browsing context.');


testInIFrame('../../resources/x-element.html', function(doc) {
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-element', {prototype: proto});

    var customElement = doc.querySelector('#x-element');
    customElement.classList.add('someClass');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1, 
        'Callback attributeChanged should be called');
}, 'Test attributeChanged callback is called if attribute value is set by method classList.add(). '+
    'The document has browsing context.');


testInIFrame('../../resources/x-element.html', function(doc) {
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-element', {prototype: proto});

    var customElement = doc.querySelector('#x-element');
    customElement.classList.add('someClass');
    assert_array_equals(proto.attributeChangedCallbackArgs, ['class', null, 'someClass'],
        'Unexpected callback attributeChanged arguments');
}, 'Test attributeChanged callback arguments if attribute value is set by method classList.add(). '+
    'The document has browsing context.');


testInIFrame('../../resources/x-element.html', function(doc) {
    var customElement = doc.querySelector('#x-element');
    // this call shouldn't invoke or enqueue the callback
    customElement.setAttribute('name', 'someName');

    var proto = newHTMLElementPrototype();
    doc.registerElement('x-element', {prototype: proto});
    assert_equals(proto.attributeChangedCallbackCalledCounter, 0, 
        'Callback attributeChanged should not be called');
    // this one should
    customElement.setAttribute('class', 'someClass');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1, 
        'Callback attributeChanged should be called');
}, 'Test attributeChanged callback is not called if attribute value of unresolved element '+
    'is set by method setAttribute(). The document has browsing context.');


testInIFrame('../../resources/x-element.html', function(doc) {
    HTML5_ELEMENTS.forEach(function(element) {
        var obj = doc.createElement(element);
        var proto = newCustomElementPrototype(obj.constructor.prototype);
        var GeneratedConstructor = doc.registerElement('x-' + element + '-' + element + '-1', {
            prototype: proto,
            extends: element
        });

        var customElement = new GeneratedConstructor();
        customElement.setAttribute('class', 'someClass');
        assert_equals(proto.attributeChangedCallbackCalledCounter, 1, 
            'Callback attributeChanged should be called');
    });
}, 'Test attributeChanged callback is called if attribute value is set by method setAttribute(). '+
    'The document has browsing context. The custom element extends some HTML element.');


testInIFrame('../../resources/x-element.html', function(doc) {
    HTML5_ELEMENTS.forEach(function(element) {
        var obj = doc.createElement(element);
        var proto = newCustomElementPrototype(obj.constructor.prototype);
        var GeneratedConstructor = doc.registerElement('x-' + element + '-' + element + '-2', {
            prototype: proto,
            extends: element
        });

        var customElement = new GeneratedConstructor();
        customElement.setAttribute('class', 'someClass');
        assert_array_equals(proto.attributeChangedCallbackArgs, ['class', null, 'someClass'],
            'Unexpected callback attributeChanged arguments');
    });
}, 'Test attributeChanged callback arguments if attribute value is set by method setAttribute(). '+
    'The document has browsing context. The custom element extends some HTML element.');
</script>
</body>
</html>
